package com.xmtlxx.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.oms.domain.Staging;
import com.xmtlxx.oms.domain.vo.StagingVo;
import com.xmtlxx.oms.mapper.StagingMapper;
import com.xmtlxx.wms.domain.DealDtl;
import com.xmtlxx.wms.domain.bo.DealDtlBo;
import com.xmtlxx.wms.domain.vo.DealDtlVo;
import com.xmtlxx.wms.mapper.DealDtlMapper;
import com.xmtlxx.wms.service.IDealDtlService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.system.domain.SysOss;
import com.xmtlxx.system.domain.bo.SysOssBo;
import com.xmtlxx.system.mapper.SysOssMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 处置明细Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class DealDtlServiceImpl implements IDealDtlService {

    private final DealDtlMapper baseMapper;

    private final StagingMapper stagingMapper;

    private final SysOssMapper ossMapper;

    /**
     * 查询处置明细
     *
     * @param id 处置明细主键
     * @return 处置明细
     */
    @Override
    public DealDtlVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectDealDtlVoByID(id);
    }

    @Override
    public DealDtlVo queryByBo(DealDtlBo bo) {
        LambdaQueryWrapper<DealDtl> lqw = buildQueryWrapper(bo);
        DealDtlVo dtlVo = baseMapper.selectVoOne(lqw);
        return dtlVo;
    }

    /**
     * 查询处置明细列表
     *
     * @param bo 处置明细
     * @return 处置明细
     */
    @Override
    public TableDataInfo<DealDtlVo> queryPageList(DealDtlBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<DealDtl> lqw = buildQueryWrapper(bo);
       // Page<DealDtlVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<DealDtlVo> result = baseMapper.selectPageDealDtlList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<DealDtlVo> pageList(DealDtlBo bo, PageQuery pageQuery) {
        // LambdaQueryWrapper<DealDtl> lqw = buildQueryWrapper(bo);
        // Page<DealDtlVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<DealDtlVo> result = baseMapper.pageDealDtlList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<DealDtlVo> dealDtlListNew(DealDtlBo bo, PageQuery pageQuery) {
        Page<DealDtlVo> result = baseMapper.getDealDtlList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询处置明细列表
     *
     * @param bo 处置明细
     * @return 处置明细
     */
    @Override
    public List<DealDtlVo> queryList(DealDtlBo bo) {
        List<DealDtlVo> list = baseMapper.selectDealDtlList(bo);
        if(list != null && list.size() > 0){
            for (DealDtlVo dtl:list){
                StagingVo stagingVo = stagingMapper.selectVoOne(new LambdaQueryWrapper<Staging>().eq(Staging::getPno,dtl.getPno()));
                if(stagingVo != null){
                    SysOssBo ossBo = new SysOssBo();
                    ossBo.setObjid(stagingVo.getId());
                    ossBo.setBuss("staging");
                    List<SysOss> oss = ossMapper.selectSysOssList(ossBo);
                    if(oss != null && oss.size() > 0){
                        dtl.setOss(oss);
                    }
                }
            }
        }
       // LambdaQueryWrapper<DealDtl> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return list;
    }

    @Override
    public TableDataInfo<DealDtlVo> warnDealDtl(DealDtlBo bo, PageQuery pageQuery) {
        Page<DealDtlVo> result = baseMapper.warnDealDtl(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<DealDtl> buildQueryWrapper(DealDtlBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DealDtl> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDealId()), DealDtl::getDealId, bo.getDealId());
        lqw.eq(StringUtils.isNotBlank(bo.getPno()), DealDtl::getPno, bo.getPno());
        lqw.like(StringUtils.isNotBlank(bo.getName()), DealDtl::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getDealType()), DealDtl::getDealType, bo.getDealType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), DealDtl::getStatus, bo.getStatus());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            DealDtl::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), DealDtl::getCreateBy, bo.getCreateBy());
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            DealDtl::getUpdateTime ,params.get("beginUpdateTime"), params.get("endUpdateTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), DealDtl::getUpdateBy, bo.getUpdateBy());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), DealDtl::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增处置明细
     *
     * @param bo 处置明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(DealDtlBo bo) {
        DealDtl add = BeanUtil.toBean(bo, DealDtl.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改处置明细
     *
     * @param bo 处置明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(DealDtlBo bo) {
        DealDtl update = BeanUtil.toBean(bo, DealDtl.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(DealDtl entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除处置明细
     *
     * @param ids 需要删除的处置明细主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
